package com.dstation.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dstation.dao.TagInfoMapper;
import com.dstation.domain.TagInfo;
import com.dstation.service.interf.ITagService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;

@Service
public class TagServiceImpl implements ITagService {

    @Resource
    private TagInfoMapper tagInfoMapper;

    @Override
    public boolean queryIsTagNameRepeat(String tagName) {
        return tagInfoMapper.selectTagNameRepeat(tagName)!=null;
    }

    @Override
    public boolean addTag(TagInfo tagInfo) {
        Timestamp time=new Timestamp(System.currentTimeMillis());

        return tagInfoMapper.insertTag(tagInfo,time)==1;
    }

    @Override
    public boolean alterTag(TagInfo tagInfo) {
        return tagInfoMapper.updateTag(tagInfo)==1;
    }

    @Override
    public boolean dropTag(long tagId) {
        return tagInfoMapper.deleteTag(tagId)==1;
    }

    @Override
    public List<TagInfo> findAllTagInfo() {
        return tagInfoMapper.selectAllTagInfo();
    }

    @Override
    public TagInfo findTagInfoByTagName(String tagName) {
        return tagInfoMapper.selectTagInfoByTagName(tagName);
    }

    @Override
    public IPage<TagInfo> findShowTags(String search,Page page) {
        if(Objects.isNull(search)){
            return tagInfoMapper.selectShowTag(search,page);
        }
        return tagInfoMapper.selectShowTag("%"+search+"%",page);
    }

    @Override
    public IPage<TagInfo> findTagSByTagName(String name,Page page) {

        return tagInfoMapper.selectTagsByName("%"+name+"%",page);
    }

    @Override
    public long findTagIdByTagName(String oldName) {
        return tagInfoMapper.selectTagIdByName(oldName);
    }

    @Override
    public List<TagInfo> findTagsByVideoId(int videoId) {
        return tagInfoMapper.selectTagsByVideoId(videoId);
    }
}
